﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using StockFinder.Indicators.Day;
using StockFinder.Indicators.Day.ClosePrice;
using StockFinder.Indicators.Day.Volume;
using StockFinder.Model;
using StockFinder.Model.Constants;
using StockFinder.NorgatePremiumData;
using System.Diagnostics;
using StockFinder.Services.PriceDatabaseAnalysers;

namespace Testharness
{
    public partial class PriceAnalyserTestForm : Form
    {
        private ExponentialMovingAverageWilderRelativeStrengthIndexClosePriceDayIndicator rsi = new ExponentialMovingAverageWilderRelativeStrengthIndexClosePriceDayIndicator(14, DayIndicatorNames.CLOSE_RSI14);
        private ExponentialMovingAverageTrueRangeClosePriceDayIndicator atr = new ExponentialMovingAverageTrueRangeClosePriceDayIndicator(14, DayIndicatorNames.CLOSE_EMATR14);        
        private NorgatePremiumDataStockFilePathsExtractor norgate = new NorgatePremiumDataStockFilePathsExtractor();

        private AllTimeHighBreakoutPriceDatabaseAnalyser athAnalyser = new AllTimeHighBreakoutPriceDatabaseAnalyser();

        public PriceAnalyserTestForm()
        {
            InitializeComponent();
        }

        private List<DailyPrice> GetTestPrices()
        {
            return norgate.GetStockPriceHistoryFromFilename("../../../Common/ASPS.csv");
        }

        private IEnumerable<IDailyPriceIndicator> AllIndicators()
        {
            return new List<IDailyPriceIndicator>()
            {
                TestCommon.CloseSMA10DayIndicator, 
                TestCommon.CloseSMA20DayIndicator, 
                TestCommon.CloseSMA50DayIndicator, 
                TestCommon.CloseSMA200DayIndicator, atr, rsi, 
                SimpleMovingAverageDailyVolumeDayIndicator.Quarter
            };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var filenames = norgate.GetStockPriceHistoryFilenamesForSpecificDirectoryName("NASDAQ");
            List<DailyPrice> priceHistory = null;
            var indicators = AllIndicators();
            int fileCount = filenames.Count;

            Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < fileCount; i++)
            {
                priceHistory = norgate.GetStockPriceHistoryFromFilename(filenames[i]);

                //apply indicators
                foreach (var item in indicators)
                {
                    item.ApplyIndicator(priceHistory);
                }

                //run analysers
                athAnalyser.AnalysePrices(priceHistory);

                Debug.WriteLine(string.Format("{0}% completed", Math.Round(((decimal)i / (decimal)fileCount) * 100, 2)));
            }

            sw.Stop();
            Debug.WriteLine(string.Format("{0}:{1} minutes taken", sw.Elapsed.Minutes, sw.Elapsed.Seconds));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            /* get the dates a stock reaches ATH */

            var prices = GetTestPrices();            

            int priceCount = prices.Count;
            List<DailyPrice> samplePrices = null;
            var results = new List<PriceDatabaseAnalyserResult>();
            PriceDatabaseAnalyserResult result = null;
            for (int i = 0; i < priceCount; i++)
            {
                samplePrices = prices.Take(i).ToList();

                result = athAnalyser.AnalysePrices(samplePrices);

                if (result.Succeeded)
                {
                    results.Add(result);

                    Debug.WriteLine(samplePrices.Last().PriceDate.ToString("dd/MM/yyyy"));
                }
            }            
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
    }
}
